<template>
  <!-- 项目类型 -->
  <div class="app-container">
    <div class="form-container">
      <el-form ref="filters" :inline="true" label-width="80px" :model="filters">
        <el-form-item label="类型名称">
          <el-input v-model="filters.typeName" size="small" placeholder="请输入类型名称" clearable />
        </el-form-item>
      </el-form>
    </div>
    <div class="btns-group">
      <superviseBtns :single="isAmend" has-permi="proType" />
    </div>
    <!-- 树形表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData"
      stripe
      :row-style="rowClass"
      class="table-list"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column type="index" label="序号" width="51" align="center" />
      <el-table-column
        prop="typeName"
        align="center"
        label="类型名称"
        min-width="180"
        :show-overflow-tooltip="true"
      />
      <el-table-column prop="orderNum" align="center" label="排序" min-width="40" />
      <el-table-column
        prop="remark"
        :show-overflow-tooltip="true"
        label="备注"
        min-width="180"
        align="center"
      />
    </el-table>
    <!-- 树形表格展示end-->
    <!-- 分页-->
    <pagination
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />

    <!-- 项目新增弹框 -->
    <addDialog ref="addDialog" />
    <!-- 项目详情弹框 -->
    <detailDialog ref="detailDialog" />
  </div>
</template>

<script>
import { listProjectType, removeProjectType } from '@/api/superviseBaseInfo/typeManage/projectType'
// 按钮组件
import superviseBtns from '@/components/Common/Btns/superviseBtns'
// 新增弹框
import addDialog from './components/addDialog'
// 详情弹框
import detailDialog from './components/detailDialog'
import { mapState } from 'vuex'

export default {
  components: { superviseBtns, addDialog, detailDialog },
  data() {
    return {
      // 高级设置显示隐藏
      highLevelSet: false,
      // 修改按钮是否可用
      isAmend: true,
      // 筛选条件
      filters: {
        pageNum: 1,
        pageSize: 20,
        typeName: ''
      },
      /* 列表 start */
      // 高亮数组
      selectRow: [],
      rowTableData: [],
      tableData: [],
      total: 0,
      // 选中行的id
      multipleSelection: [],
      // 表格加载是否显示等待图标
      loading: false
      /* 列表 end */
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    const _this = this // 赋值vue的this
    window.onresize = () => {
      // 调用methods中的事件
      _this.AddTaleData(_this.tableData, _this.total)
    }
    // 跳转路由时关闭窗口提交
    this.$once('hook:beforeDestroy', () => {
      window.onresize = null
    })
  },
  methods: {
    // 加载树形表格
    async getList() {
      this.loading = true
      try {
        const { data: { rows, total }} = await listProjectType(this.filters)
        this.tableData = rows
        this.rowTableData = []
        this.total = total
        // this.addTaleData(this.tableData)
        this.loading = false
      } catch {
        this.loading = false
      }
    },
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // list表格填充空白行
    addTaleData() {
      if (this.tableData.length < this.fristPageSize) {
        const arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach((item) => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }
      if (val.length === 1) {
        this.isAmend = false
      } else {
        this.isAmend = true
      }
      this.selectRow = []
      this.multipleSelection = val
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    /* 表单选中事件 end */

    /* 按钮  start */
    // 搜索方法
    handleQuery() {
      this.getList()
    },
    // 重置
    resetQuery() {
      this.resetForm('filters')
      this.filters = {}
      this.getList()
    },
    // 点击添加菜单弹出的对话框
    handleAdd() {
      this.$refs.addDialog.handleAdd('新增项目类型')
    },
    // 编辑操作
    handleUpdate() {
      // 获取选中行的id
      this.$refs.addDialog.handleAdd('修改项目类型', this.multipleSelection[0].id)
    },
    // 详情
    handleDetail() {
      this.$refs.detailDialog.handleAdd('项目类型详情', this.multipleSelection[0].id)
    },
    // 删除操作
    handleDelete() {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await removeProjectType(this.multipleSelection[0].id)
          this.msgSuccess('删除成功!')
          this.getList()
        })
        .catch(() => {})
    }
    /* 按钮 end */
  }
}
</script>

<style scoped>
</style>
